<?xml version="1.0"?>

<!--
    Copyright (C) 2010 J. Elfring


    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:at="http://www.w3.org/2005/Atom" xmlns:gd="http://schemas.google.com/g/2005" version="1.0">
<xsl:output method="text" version="1.0" encoding="iso-8859-1" indent="no"/>

<!-- NewLine for later reuse -->
<xsl:variable name="newLine"><xsl:text>
</xsl:text></xsl:variable>

  <xsl:template match="/">
    <xsl:text disable-output-escaping="yes">[Backup]</xsl:text>
    <xsl:value-of select="$newLine"/>
    <xsl:text disable-output-escaping="yes">Format = 1.04</xsl:text>
    <xsl:value-of select="$newLine"/>
    <xsl:text disable-output-escaping="yes"></xsl:text>
    <xsl:value-of select="$newLine"/>
    <!-- Continue if the entry containes at least one interesting phone number -->
    <xsl:apply-templates select="at:feed/at:entry[gd:phoneNumber/@rel='http://schemas.google.com/g/2005#mobile' or gd:phoneNumber/@rel='http://schemas.google.com/g/2005#home' or gd:phoneNumber/@rel='http://schemas.google.com/g/2005#work']"/>
  </xsl:template>

  <xsl:template match="at:feed/at:entry">
    <!-- if thre are multiple number of one type (rel) use only the first one -->
    <xsl:apply-templates select="gd:phoneNumber[not(@rel=preceding-sibling::gd:phoneNumber/@rel)]"/>
  </xsl:template>

  <xsl:template match="gd:phoneNumber">
    <!-- evaluate type of number -->
    <xsl:variable name="ctype">
      <xsl:choose>
        <xsl:when test="@rel = 'http://schemas.google.com/g/2005#mobile'">M</xsl:when>
        <xsl:when test="@rel = 'http://schemas.google.com/g/2005#home'">H</xsl:when>
        <xsl:when test="@rel = 'http://schemas.google.com/g/2005#work'">W</xsl:when>
        <xsl:otherwise>SKIP</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <!-- Contactname -->
    <xsl:variable name="title"><xsl:value-of select="../at:title"/></xsl:variable>
    <xsl:variable name="lname">   
        <xsl:choose>
            <xsl:when test="contains($title,' ')">
                <xsl:variable name="nname"><xsl:value-of select="substring-after($title,' ')"/></xsl:variable>
                <xsl:variable name="vname"><xsl:value-of select="substring($title,1,1)"/></xsl:variable>
                <xsl:value-of select="concat($vname,' ',$nname)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$title"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>
	<!-- cut max length (30 - space - type) -->
    <xsl:variable name="cname"><xsl:value-of select="concat(substring($lname,1,28),' ',$ctype)"/></xsl:variable>
    <!-- entry count -->
    <xsl:variable name="lnum"><xsl:number format="1" level="any"/></xsl:variable>
    <!-- remove everythin but numbers and + -->
    <xsl:variable name="cnumber">
	<xsl:value-of select = "translate(.,translate(.,'1234567890+',''),'')" />
    </xsl:variable>
    <!-- generate output -->
    <xsl:if test="$ctype != 'SKIP'">
      <xsl:text disable-output-escaping="yes">[PhonePBK</xsl:text><xsl:value-of select="$lnum"/><xsl:text disable-output-escaping="yes">]</xsl:text>
      <xsl:value-of select="$newLine"/>
      <xsl:text disable-output-escaping="yes">Location = </xsl:text><xsl:value-of select="$lnum"/>
      <xsl:value-of select="$newLine"/>
      <xsl:text disable-output-escaping="yes">Entry00Type = NumberGeneral</xsl:text>
      <xsl:value-of select="$newLine"/>
      <xsl:text disable-output-escaping="yes">Entry00Text = "</xsl:text><xsl:value-of select="$cnumber"/><xsl:text disable-output-escaping="yes">"</xsl:text>
      <xsl:value-of select="$newLine"/>
      <xsl:text disable-output-escaping="yes">Entry01Type = Name</xsl:text>
      <xsl:value-of select="$newLine"/>
      <xsl:text disable-output-escaping="yes">Entry01Text = "</xsl:text><xsl:value-of select="$cname"/><xsl:text disable-output-escaping="yes">"</xsl:text>
      <xsl:value-of select="$newLine"/>
      <xsl:value-of select="$newLine"/>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>
